package com.yunxi.service.dubboService.impl.sto;

import com.alibaba.cloud.context.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yunxi.hubToBiz.pojo.VdcVehicleCheckVO;
import com.yunxi.hubToBiz.service.VdcVehicleCheckService;
import com.yunxi.model.sto.*;
import com.yunxi.service.localService.sto.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 道闸验证接口 服务实现类
 * </p>
 *
 * @author caoYong
 * @since 2019-06-04
 */

public class VdcVehicleCheckServiceImpl implements VdcVehicleCheckService {

    @Autowired
    @Qualifier(value = "stoTransportOrderServiceImpl")
    StoTransportOrderService stoTransportOrderService;

    @Autowired
    @Qualifier(value = "stoStockInPlanServiceImpl")
    StoStockInPlanService stoStockInPlanService;

    @Autowired
    @Qualifier(value = "stoStockOutPlanServiceImpl")
    StoStockOutPlanService stoStockOutPlanService;

    @Autowired
    @Qualifier(value = "stoVehicleLendOrderServiceImpl")
    StoVehicleLendOrderService stoVehicleLendOrderService;

    @Autowired
    @Qualifier(value = "vehicleOfflineServiceImpl")
    VehicleOfflineService vehicleOfflineService;


    /**
     * 道闸验证
     * @param vdcVehicleCheckVO v
     * @return vo
     * @throws Exception e
     */
    @Override
    public VdcVehicleCheckVO vdcVehicleCheck(VdcVehicleCheckVO vdcVehicleCheckVO) throws Exception {
        String platesNo = vdcVehicleCheckVO.getPlatesNo();//车牌号/入库人员RFID,20191210变更为车辆RFID
        String dateTime = vdcVehicleCheckVO.getTimeStamp();
        String roadGateNo = vdcVehicleCheckVO.getRoadGateNo();//道闸编号
        String type = vdcVehicleCheckVO.getType();// OUT/IN
        //1.根据车牌号查询运输计划
        QueryWrapper<StoTransportOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("TRANSPORT_LICENSE", platesNo);
        queryWrapper.ne("ORDER_STATE", 3);//排除已全部接车,作废的运单
        queryWrapper.ne("ORDER_STATE", 99);
        StoTransportOrder stoTransportOrder = stoTransportOrderService.getOne(queryWrapper);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        //首先把platesNo当车牌号去查找运单
        if (stoTransportOrder != null) {
            Date planArrivedTime = stoTransportOrder.getPlanArrivedTime();//预计板车到达工厂时间
            vdcVehicleCheckVO.setStatus(1);
            vdcVehicleCheckVO.setRoad(stoTransportOrder.getShippingRoad());//发运道次
            vdcVehicleCheckVO.setMessage("验证成功!");
        }
        //找不到运单则,把 platesNo 当入库人员RFID去查找状态为1准备入库的入库计划
        //20191210变更为车辆RFID
        else {
            String businessKey = null;
            QueryWrapper<VehicleOffline> queryWrapper3 = new QueryWrapper<>();
            queryWrapper3.eq("VEHICLE_RFID",platesNo);
            VehicleOffline vehicleOffline = vehicleOfflineService.getOne(queryWrapper3);
            if(vehicleOffline != null){
                businessKey = vehicleOffline.getVin();
            }
            //入道闸
            if(StringUtils.isEmpty(type) || type.equalsIgnoreCase("IN")){
                QueryWrapper<StoStockInPlan> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("VIN", businessKey);
                queryWrapper1.in("BILL_STATE", "1","5");
                List<StoStockInPlan> list = stoStockInPlanService.list(queryWrapper1);
                if (list == null || list.size() <= 0) {
                    vdcVehicleCheckVO.setStatus(2);
                    vdcVehicleCheckVO.setMessage("验证失败,无法根据RFID号:" + platesNo + "找到对应准备入库的计划!");
                } else {
                    vdcVehicleCheckVO.setStatus(1);
                    vdcVehicleCheckVO.setMessage("验证成功!");
                    //借车入库/领用入库类型的入库单,入道闸后回写借车单单据状态为已入库
                    StoStockInPlan stoStockInPlan = list.get(0);
                    String stockInType = stoStockInPlan.getStockInType();
                    String vin = stoStockInPlan.getVin();
                    stoStockInPlan.setVdcInTime(new Date());
                    stoStockInPlanService.updateById(stoStockInPlan);//入VDC道闸时间写入入库计划
                    if(stockInType != null && (stockInType.equals("6") || stockInType.equals("14"))){
                        QueryWrapper<StoVehicleLendOrder> queryWrapper2 = new QueryWrapper<>();
                        queryWrapper2.eq("VIN",vin);
                        queryWrapper2.eq("ORDER_STATE",2);//借车已出库
                        StoVehicleLendOrder stoVehicleLendOrder = stoVehicleLendOrderService.getOne(queryWrapper2);
                        if(stoVehicleLendOrder != null){
                            stoVehicleLendOrder.setOrderState(3);//车辆已入库
                            stoVehicleLendOrderService.updateById(stoVehicleLendOrder);
                        }
                    }
                }
            }
            //出道闸
            else if(type != null && type.equalsIgnoreCase("OUT")){
                //根据RFID查询出库计划
                QueryWrapper<StoStockOutPlan> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("VIN",businessKey);
                queryWrapper1.ne("BILL_STATE","4");
                queryWrapper1.ne("BILL_STATE","5");
                List<StoStockOutPlan> list = stoStockOutPlanService.list(queryWrapper1);
                if(list == null || list.size() <= 0){
                    vdcVehicleCheckVO.setStatus(2);
                    vdcVehicleCheckVO.setMessage("验证失败,无法根据RFID号:" + platesNo + "找到对应准备出库的计划!");
                }
                else{
                    StoStockOutPlan stoStockOutPlan = list.get(0);
                    //20191129增加验证,编号DZ006(PDI棚处出库道闸),仅允许借用、领用、返修、返厂类型出库
                    String stockOutType = stoStockOutPlan.getStockOutType();
                    if(StringUtils.isNotEmpty(roadGateNo) && roadGateNo.equalsIgnoreCase("DZ006")){
                        if(StringUtils.isNotEmpty(stockOutType)
                                && !stockOutType.equals("4")
                                && !stockOutType.equals("13")
                                && !stockOutType.equals("12")
                                && !stockOutType.equals("14")){
                            vdcVehicleCheckVO.setStatus(2);
                            vdcVehicleCheckVO.setMessage("验证失败,DZ006道闸仅允许借用,领用,返修,出厂类型的出库!");
                            return vdcVehicleCheckVO;
                        }
                    }

                    vdcVehicleCheckVO.setStatus(1);
                    vdcVehicleCheckVO.setMessage("验证成功!");
                    stoStockOutPlan.setVdcOutDate(new Date());
                    //更新出库计划出道闸时间
                    stoStockOutPlanService.saveOrUpdate(stoStockOutPlan);


                    String vin = stoStockOutPlan.getVin();
                    //如果出库类型为借车出库,则更新借车单状态
                    if(StringUtils.isNotEmpty(stockOutType) && stockOutType.equals("4")){
                        QueryWrapper<StoVehicleLendOrder> queryWrapper2 = new QueryWrapper<>();
                        queryWrapper2.eq("VIN",vin);
                        queryWrapper2.eq("ORDER_STATE",1);//借车已出库
                        StoVehicleLendOrder stoVehicleLendOrder = stoVehicleLendOrderService.getOne(queryWrapper2);
                        if(stoVehicleLendOrder != null){
                            stoVehicleLendOrder.setOrderState(2);//车辆已出库
                            stoVehicleLendOrderService.updateById(stoVehicleLendOrder);
                        }
                    }
                }
            }
        }
        return vdcVehicleCheckVO;
    }
}
